/**
 * <h3>SuggestionController</h3>
 * <p>建议信息管理控制器类</p>
 *
 * <h3>功能描述</h3>
 * <p>提供建议信息的增删改查等基础操作接口，统一返回AjaxResult格式结果</p>
 * <p>支持建议的创建、编辑、删除、查询等核心功能，包含分页查询和详细信息获取</p>
 * <p>所有接口均基于RESTful风格设计，配合SpringMVC与Swagger注解实现标准化API输出</p>
 *
 * <h3>元数据</h3>
 * <ul>
 *    <li>作者：郭强</li>
 *    <li>创建日期：2020年05月22日 21:09:09</li>
 * </ul>
 *
 * <h3>其他信息</h3>
 * <ul>
 *    <li>参考链接：<a href="https://fantastic-admin.hurui.me/guide/permission.html">权限控制文档</a></li>
 *    <li>示例代码：{@code new SuggestionController(suggestionService)} </li>
 * </ul>
 *
 * @apiNote 接口需结合前端权限配置使用，确保安全性；当前部分接口已启用权限注解
 * @implSpec 继承自 {@link BaseController}，复用分页、返回封装等通用逻辑
 * @since 1.0.0
 */

package com.dnndo.web.controller.system;


import com.dnndo.common.annotation.Log;
import com.dnndo.common.core.controller.BaseController;
import com.dnndo.common.core.domain.AjaxResult;
import com.dnndo.common.core.domain.PageResult;
import com.dnndo.common.core.domain.R;
import com.dnndo.common.enums.BusinessType;
import com.dnndo.common.utils.bean.BeanDtoVoUtil;
import com.dnndo.system.domain.Suggestion;
import com.dnndo.system.domain.dto.SuggestionDTO;
import com.dnndo.system.domain.vo.SuggestionVO;
import com.dnndo.system.service.ISuggestionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 建议信息controller类
 *
 * @author 郭强
 *
 */
@RestController
@RequestMapping("/system/suggestion")
public class SuggestionController extends BaseController {


    private final ISuggestionService suggestionService;

    @Autowired
    public SuggestionController(ISuggestionService suggestionService) {
        this.suggestionService = suggestionService;
    }

    /**
     * 查询建议列表（分页），使用模糊查询
     * @param dto 建议查询条件封装对象，包含用户ID等过滤条件
     * @return PageResult<SuggestionVO> 分页后的建议信息列表及总记录数
     * @see com.dnndo.system.domain.vo.SuggestionVO
     */
    @PreAuthorize("@ss.hasPermi('system:suggestion:list')")
    @GetMapping("/list")
    public PageResult<SuggestionVO> list(SuggestionDTO dto) {
        startPage();
        dto.setUserId(String.valueOf(getUserId().intValue()));
        List<Suggestion> list = suggestionService.selectSuggestionList(dto);
        return PageResult.pageOk(BeanDtoVoUtil.listVo(list, SuggestionVO.class), new PageInfo<>(list).getTotal());
    }

    /**
     * 根据建议ID获取详细信息
     * @param suggestionId 建议唯一标识符
     * @return AjaxResult 包含建议详细信息的响应结果
     * @see com.dnndo.system.domain.vo.SuggestionVO
     */
    @PreAuthorize("@ss.hasPermi('system:suggestion:getInfo')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@ApiParam(value = "建议ID", required = true) Long suggestionId) {
        Long id = getUserId();
        SuggestionDTO suggestion = suggestionService.selectSuggestionListById(suggestionId,id);
        return AjaxResult.success(R.ok(BeanDtoVoUtil.convert(suggestion, SuggestionVO.class)));
    }

    /**
     * 新增建议信息
     * @param dto 前端传入的建议数据传输对象，包含新增字段
     * @return AjaxResult 操作结果返回对象，包含成功或失败状态
     * @see com.dnndo.system.domain.dto.SuggestionDTO
     */
    @PreAuthorize("@ss.hasPermi('system:suggestion:add')")
    @Log(title = "建议管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SuggestionDTO dto) {
        dto.setUserId(String.valueOf(getUserId().intValue()));
        return toAjax(suggestionService.insertSuggestion(dto));
    }

    /**
     * 新增建议信息（无权限控制）
     * @param dto 前端传入的建议数据传输对象，包含新增字段
     * @return AjaxResult 操作结果返回对象，包含成功或失败状态
     * @see com.dnndo.system.domain.dto.SuggestionDTO
     */
    @PostMapping("/suggestionAdd")
    public AjaxResult suggestionAdd(@Validated @RequestBody SuggestionDTO dto) {
        dto.setUserId(String.valueOf(getUserId().intValue()));
        return toAjax(suggestionService.insertSuggestion(dto));
    }

    /**
     * 修改已有建议信息
     * @param dto 前端传入的建议数据传输对象，包含更新字段
     * @return AjaxResult 操作结果返回对象，包含成功或失败状态
     * @see com.dnndo.system.domain.dto.SuggestionDTO
     */
    @PreAuthorize("@ss.hasPermi('system:suggestion:edit')")
    @Log(title = "建议管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SuggestionDTO dto) {
        // 设置当前用户ID
        dto.setUserId(String.valueOf(getUserId()));
        SuggestionDTO suggestionDTO = BeanDtoVoUtil.convert(dto, SuggestionDTO.class);
        return toAjax(suggestionService.updateSuggestionById(suggestionDTO));
    }

    /**
     * 删除指定ID的建议信息
     * @param suggestionId 要删除的建议唯一标识符
     * @return AjaxResult 操作结果返回对象，包含成功或失败状态
     * @see com.dnndo.system.domain.dto.SuggestionDTO
     */
    @PreAuthorize("@ss.hasPermi('system:suggestion:del')")
    @Log(title = "建议管理", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    public AjaxResult del(@ApiParam(value = "建议ID",required = true) Integer suggestionId)
    {
        return toAjax(suggestionService.deleteSuggestionById(suggestionId));
    }

}
